from flask import g, jsonify
import time
from myapp import app, appbuilder
from flask_appbuilder import expose, BaseView

class FlowiseView(BaseView):
    route_base = '/flowise'
    
    @expose('/auth', methods=['POST'])
    def auth(self):
        """为 Flowise 提供认证信息"""
        try:
            # 获取当前用户信息
            user = g.user if hasattr(g, 'user') else None
            if not user:
                return jsonify({'error': 'User not authenticated'}), 401
            
            # 构造认证信息
            auth_data = {
                'username': user.username,
                'user_id': user.id,
                'roles': [role.name for role in user.roles],
                'timestamp': int(time.time())
            }
            
            return jsonify(auth_data)
        except Exception as e:
            app.logger.error(f"Flowise auth error: {str(e)}")
            return jsonify({'error': 'Authentication failed'}), 500

appbuilder.add_view_no_menu(FlowiseView)